//
//  MyWaterFLow.m
//  iphoneApp
//
//  Created by Liyu on 12-11-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MyWaterFLow.h"
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "MyImageView.h"
#import "UIAlertView+SimpleAlert.h"



#define ColumnSpace 2.0

#define PageSize 20

@implementation MyWaterFLow

{
    
}

@synthesize mywaterFlowDelegate=_mywaterFlowDelegate;
@synthesize pageIndex=_pageIndex;
@synthesize ColumnWidth=_columnWidth;
@synthesize columnHeightArr;





- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self CalucateColumnWidth];
        self.pageIndex=1;
        self.delegate=self;
        columnHeightArr=[[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInteger:2],
                         [NSNumber numberWithInteger:2],[NSNumber numberWithInteger:2], nil ];
    }
    return self;
}



-(void) CalucateColumnWidth
{
    self.ColumnWidth=(320-4*ColumnSpace)/3.0;
}


-(void)setDataSource:(id<MyWaterFlowDataSource>)dataSource
{
    _dataSource=dataSource;
    //[self LoadWaterFlow];
}


-(id<MyWaterFlowDataSource>) dataSource
{
    return _dataSource;
}



-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{    
    [self.mywaterFlowDelegate myWaterFlowDidScroll:self];  	
}


-(void)LoadWaterFlow
{
    NSInteger counts=[self.dataSource numberOfImageInWaterFlow:self]; 
    
    
    BOOL isLoad=NO;
    NSInteger begin=(self.pageIndex-1)*PageSize;
    
        for (NSInteger i=begin; i<counts; i++) {
        NSInteger columnIndex=[self getMinHeightColumnIndexInArray];
        NSInteger imgPointY=[[columnHeightArr objectAtIndex:columnIndex] integerValue];
        NSInteger height=[self.dataSource waterflow:self heightForImageAtIndex:i];  
            if (height>self.frame.size.height) {
                height=self.frame.size.height;
            }
                                              
        MyImageView * view=[self.dataSource waterflow:self imageViewForWaterFlowAtIndex:i];
        view.tag=i;
        view.contentMode=UIViewContentModeScaleAspectFill;
        view.layer.borderColor=[UIColor colorWithWhite:0.5 alpha:0.5].CGColor;
        view.layer.borderWidth=0.5;
        view.layer.cornerRadius=5.0;
        view.layer.masksToBounds=YES;
        
        view.frame=CGRectMake(columnIndex*self.ColumnWidth+(columnIndex*ColumnSpace+ColumnSpace), 
                              imgPointY, self.ColumnWidth, height);
        [columnHeightArr replaceObjectAtIndex:columnIndex withObject:[NSNumber numberWithInteger:(height+imgPointY+3)]];
        
        
        [self addSubview:view];
       
        isLoad=YES;
    }
    
    if (self.pageIndex==1) {
        self.bounces=NO;
         self.contentSize=CGSizeMake(320,[[columnHeightArr objectAtIndex:[self getMaxHeightColumnIndexInArray]] integerValue]);
        self.layer.backgroundColor=[UIColor whiteColor].CGColor;
  
        self.showsHorizontalScrollIndicator=NO;
        self.showsVerticalScrollIndicator=YES;
    }    
    else {
        if (isLoad) {
            self.contentSize=CGSizeMake(320,[[columnHeightArr objectAtIndex:[self getMaxHeightColumnIndexInArray]] integerValue]);
        }
        else {
            self.pageIndex=self.pageIndex-1;
        }
    }
        
}


-(NSInteger) getMinHeightColumnIndexInArray
{
    NSInteger  min=[[columnHeightArr objectAtIndex:0] integerValue];
    NSInteger index=0;
    for (NSInteger i=1; i<[columnHeightArr count]; i++) {
        if ([[columnHeightArr objectAtIndex:i] integerValue]<min) {
            min=[[columnHeightArr objectAtIndex:i] integerValue];
            index=i;
        }
    }
    return index;
}

-(NSInteger) getMaxHeightColumnIndexInArray
{
    NSInteger max=[[columnHeightArr objectAtIndex:0] integerValue];
    NSInteger index=0;
    for (NSInteger i=1; i<[columnHeightArr count]; i++) {
        if ([[columnHeightArr objectAtIndex:i] integerValue]>max) {
            max=[[columnHeightArr objectAtIndex:i] integerValue];
            index=i;
        }
    }
    return index;
}


-(void) dealloc
{
    
    columnHeightArr=nil;
    [super dealloc];
}




/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
